package entity

import (
	"encoding/json"
	"time"
)

// 是否双倍
type Double struct {
	RoleID    int32  `gorm:"type:int;primary_key"`
	Data      []byte `gorm:"type:jsonb"`
	CreatedAt time.Time
	UpdatedAt time.Time
	data      map[int32]bool `sql:"-"`
}

func (e *Double) AfterFind() error {

	e.data = make(map[int32]bool)

	_ = json.Unmarshal(e.Data, &e.data)
	return nil
}

func (e *Double) BeforeSave() error {

	if e.data == nil {
		e.data = make(map[int32]bool)
	}

	e.Data, _ = json.Marshal(e.data)
	return nil
}

func (e *Double) BeforeUpdate() error {

	if e.data == nil {
		e.data = make(map[int32]bool)
	}

	e.Data, _ = json.Marshal(e.data)
	return nil
}

func (e *Double) HaveProduct(productID int32) bool {
	return e.data[productID]
}

func (e *Double) SetProduct(productID int32) {
	e.data[productID] = true
}
